
 

class common {
	constructor() {

	}
	ImgToBase64() {
		convertImgToBase64(obj.url, function(base64Img) {
			//转化后的base64
			obj.imgData = base64Img;
		})
	}
	//去掉空格
	trim(s) {
		return trimRight(trimLeft(s));
	}
	//这是有设定过期时间的使用示例：
	//s20是代表20秒
	//h是指小时，如12小时则是：h12
	//d是天数，30天则：d30
	//	setCookie("name","hayden","s20");
	setCookie (name,value,time){
		
		var strsec,exp;
		
		if (time) {
			strsec = getsec(time);
			exp = new Date();
			exp.setTime(exp.getTime() + strsec*1);
			document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
			
		}else{
			 
			document.cookie = name + "="+ escape (value) + ";path=/;domain=.iafclub.com";
		}
	}
	getCookie (name){
		var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
		if(arr=document.cookie.match(reg))
		return unescape(arr[2]);
		else
		return undefined;
	}
	delCookie (name){
		var exp = new Date();
		exp.setTime(exp.getTime() - 1);
		var cval=getCookie(name);
		if(cval!=null)
		document.cookie= name + "="+cval+";expires="+exp.toGMTString();
	}
	//获取域名
	gethost() {
		return "https//:" + window.location.host;
	}
	gethtPname(){
		var url = window.location.href;
		
			if(url.indexOf("?") != -1){
			    url = url.split("?")[0];
			}
		return url;	
		
	}
	//获取域名
	getPathname() {
		return window.document.location.pathname;
	}
//	获取本地数据
	gCacheData(str) {
			 
            var val = window.sessionStorage[str];
            val = val ? val : window.localStorage[str];
            return val ? JSON.parse(val) : '';
    }
	//设置本地数据
	sCacheData(obj){
    	
    	for(var i in obj){
    		localStorage[i] = JSON.stringify(obj[i]);
    	}
   }
	
	saveUserInfo(userInfo){
		//userAttribute  01 投资人  02 借款人 05担保方
		userInfo.unAccount = userInfo.isOpenAccount=='0';
		userInfo.inAccount = userInfo.isOpenAccount=='1';
		userInfo.accountFail = userInfo.isOpenAccount=='3';
		userInfo.unBindCard = userInfo.isBindCard=='0';
		userInfo.inBindCard = userInfo.isBindCard=='1';
		userInfo.bindCardFail = userInfo.isBindCard=='2';
		userInfo.userStatus = {0:'尚未开户',1:'已开户',2:'审核中',3:'开户失败'}[userInfo.isOpenAccount/1];
		userInfo.isInvest = userInfo.userAttribute=='01';
		userInfo.isLoan = userInfo.userAttribute=='02';
		userInfo.isGuart = userInfo.userAttribute=='05';
		userInfo.isloginCode = userInfo.loginCodeFlag=='1';
		this.sCacheData({userInfo});
		return userInfo;
	}
	setSMSLogin(flag){
		const userInfo =  common.gCacheData('userInfo');
		userInfo.loginCodeFlag = flag;
		saveUserInfo(userInfo);
	}
   //获取用户信息
   gUserInfo(){
    	var user = {},
    		list=['mobilePhone','userAttribute','bindCardFlag','userId','userType','token'];
    	
    	 list.forEach(function(i){
    	 	
    	 	if (localStorage[i]) {
    	 		user[i] = localStorage[i];
    	 	}
    	 })
    	
		return user;	 
    }
   //获取地址里面指定参数的值 默认返回参数集合
	GetRequest(name) {

		var url = location.search; //获取url中"?"符后的字串 
		var theRequest = {},
			strs;
			
		if (url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for (var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
			}
		}

		if (name) {
			return theRequest[name];
		}
		return theRequest;

	}
	//设置头部
	setTitle(tilte){
		 tilte= tilte?tilte:'精融汇-保险的互联网金融平台';
		 document.title = tilte;
	}
	//是否登陆
	isLogin(){
    	
    	var t = localStorage.token;
    	if (t) {
    		return true;
    	}
    	return false;
    }
	// number 数值  places 保留小数 位数 默认2   symbol 开头符号 $  thousand 分隔符号 ，   小数点  默认 .
	formatMoney (number, places, symbol, thousand, decimal) {
        number = number || 0;
        places = !isNaN(places = Math.abs(places)) ? places : 2;
        symbol = symbol !== undefined ? symbol : "$";
        thousand = thousand || ",";
        decimal = decimal || ".";
        var negative = number < 0 ? "-" : "",
            i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + "",
            j = (j = i.length) > 3 ? j % 3 : 0;
        return symbol + negative + (j ? i.substr(0, j) + thousand : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : "");
    }
	
}

//  ========== 
//  = /实现将项目的图片转化成base64 = 
//	url 图片地址 callback 回调函数 outputFormat 图片格式
//  ========== 
function convertImgToBase64(url, callback, outputFormat) {
	var canvas = document.createElement('CANVAS'),
		　　ctx = canvas.getContext('2d'),
		　　img = new Image;　　
	img.crossOrigin = 'Anonymous';　　
	img.onload = function() {　　
		canvas.height = img.height;　　
		canvas.width = img.width;　　
		ctx.drawImage(img, 0, 0);

		　　
		var dataURL = canvas.toDataURL(outputFormat || 'image/jpeg');　　
		callback.call(this, dataURL);　　
		canvas = null;
	};　　
	img.src = url;
}

function trimLeft(s) {
	if(s == null) {
		return "";
	}
	var whitespace = new String(" \t\n\r");
	var str = new String(s);
	if(whitespace.indexOf(str.charAt(0)) != -1) {
		var j = 0,
			i = str.length;
		while(j < i && whitespace.indexOf(str.charAt(j)) != -1) {
			j++;
		}
		str = str.substring(j, i);
	}
	return str;
}

function trimRight(s) {
	if(s == null) return "";
	var whitespace = new String(" \t\n\r");
	var str = new String(s);
	if(whitespace.indexOf(str.charAt(str.length - 1)) != -1) {
		var i = str.length - 1;
		while(i >= 0 && whitespace.indexOf(str.charAt(i)) != -1) {
			i--;
		}
		str = str.substring(0, i + 1);
	}
	return str;
}
function getsec(str){
		 
		var str1=str.substring(1,str.length)*1;
		var str2=str.substring(0,1);
		if (str2=="s")
		{
		return str1*1000;
		}
		else if (str2=="h")
		{
		return str1*60*60*1000;
		}
		else if (str2=="d")
		{
		return str1*24*60*60*1000;
		}
	}
	
	
	
	module.exports = new common();